package com.gsbelarus.gedemin.salary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.database.RealmHelper;
import com.gsbelarus.gedemin.salary.entity.HorizontalBarData;
import com.gsbelarus.gedemin.salary.entity.PayslipGeneralItem;
import com.gsbelarus.gedemin.salary.ui.HorizontalBarChart;
import com.gsbelarus.gedemin.salary.util.CurrencyHelper;
import com.gsbelarus.gedemin.salary.util.DateFormatHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class PayslipGeneralAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private List<PayslipGeneralItem> listItems;
    private List<HorizontalBarData> chartData;
    private Calendar date;

    private CurrencyHelper currencyHelper;
    private RealmHelper realmHelper;

    private int headerCount = 0;

    private String headerTitle;
    private double headerSum;

    public enum ItemType {
        HEADER, ITEM, SUB_ITEM
    }

    public PayslipGeneralAdapter(Context context, RealmHelper realmHelper) {
        this.context = context;
        this.realmHelper = realmHelper;
        listItems = new ArrayList<>();
        currencyHelper = CurrencyHelper.getInstance(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (ItemType.values()[viewType]) {
            case HEADER:
                return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.payslip_general_list_header, parent, false));
            case ITEM:
                return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.payslip_general_list_item, parent, false));
            case SUB_ITEM:
                return new SubItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.payslip_general_list_sub_item, parent, false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int i = position - headerCount;
        switch (ItemType.values()[getItemViewType(position)]) {
            case HEADER:
                HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
                headerViewHolder.chart.setType(HorizontalBarChart.Type.SERIES_BAR_CHART);
                headerViewHolder.chart.setData(chartData);
                headerViewHolder.chart.animate(300);
                headerViewHolder.name.setText(headerTitle + " за " + DateFormatHelper.getMonthString(context, date));
                headerViewHolder.sum.setText(
                        currencyHelper.getConvertedSumString(realmHelper, date, headerSum) +
                                currencyHelper.getKindOfCurrencyText(" "));
                break;
            case ITEM:
                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                //itemViewHolder.date.setText(listItems.get(i).getFormattedDate());
                itemViewHolder.name.setText(listItems.get(i).getName());
                itemViewHolder.sum.setText(
                        currencyHelper.getConvertedSumString(realmHelper, date, listItems.get(i).getSum()) +
                                currencyHelper.getKindOfCurrencyText(" "));
                break;
            case SUB_ITEM:
                SubItemViewHolder subItemViewHolder = (SubItemViewHolder) holder;
                subItemViewHolder.color.setBackgroundResource(listItems.get(i).getColor());
                subItemViewHolder.name.setText(listItems.get(i).getName());
                subItemViewHolder.sum.setText(
                        currencyHelper.getConvertedSumString(realmHelper, date, listItems.get(i).getSum()));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return listItems.size() + headerCount;
    }

    @Override
    public int getItemViewType(int position) {
        if (headerCount > 0 && position == 0)
            return ItemType.HEADER.ordinal();
        return (listItems.get(position - headerCount).getColor() == 0) ?
                ItemType.ITEM.ordinal() : ItemType.SUB_ITEM.ordinal();
    }

    public void showHeader(String headerTitle, double headerSum, List<HorizontalBarData> chartData) {
        this.chartData = chartData;
        this.headerTitle = headerTitle;
        this.headerSum = headerSum;
        this.headerCount = 1;
        notifyItemInserted(0);
    }

    public void hideHeader() {
        chartData = null;
        headerCount = 0;
        headerSum = 0;
        headerTitle = "";
        notifyItemRemoved(0);
    }

    public PayslipGeneralAdapter setDate(Calendar date) {
        this.date = date;
        return this;
    }

    public void setList(List<PayslipGeneralItem> listItems) {
        this.listItems = listItems;
        notifyDataSetChanged();
    }

    public PayslipGeneralAdapter addItem(PayslipGeneralItem item) {
        int position = listItems.size();
        listItems.add(position, item);
        notifyItemInserted(position + headerCount);
        return this;
    }

    public PayslipGeneralAdapter addItems(List<PayslipGeneralItem> items) {
        int position = listItems.size();
        listItems.addAll(items);
        notifyItemRangeInserted(position + headerCount, items.size());
        return this;
    }

    public void clear() {
        hideHeader();
        listItems.clear();
        notifyDataSetChanged();
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {

        private HorizontalBarChart chart;
        private TextView sum;
        private TextView name;

        HeaderViewHolder(View v) {
            super(v);
            name = (TextView) v.findViewById(R.id.payslip_general_header_item_name);
            sum = (TextView) v.findViewById(R.id.payslip_general_header_item_sum);
            chart = (HorizontalBarChart) v.findViewById(R.id.horizontal_chart);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        //private TextView date;
        private TextView name;
        private TextView sum;

        public ItemViewHolder(View v) {
            super(v);
            //     date = (TextView) v.findViewById(R.id.payslip_general_list_item_date);
            name = (TextView) v.findViewById(R.id.payslip_general_list_item_name);
            sum = (TextView) v.findViewById(R.id.payslip_general_list_item_sum);
        }
    }

    class SubItemViewHolder extends RecyclerView.ViewHolder {

        private View color;
        private TextView name;
        private TextView sum;

        public SubItemViewHolder(View v) {
            super(v);
            color = v.findViewById(R.id.payslip_general_list_sub_item_color);
            name = (TextView) v.findViewById(R.id.payslip_general_list_sub_item_name);
            sum = (TextView) v.findViewById(R.id.payslip_general_list_sub_item_sum);
        }
    }
}
